/**
 * 
 */
package view.impl.ingame;

import java.io.FileNotFoundException;

import model.IinGame;
import model.ModelFactory;

import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;

import util.ResLoc;
import view.impl.FontContainer;
import view.impl.PositionOffset;


/**
 * @author Julian
 *
 */
class StatusMeter {
	
	private float horOff;
	private float verOff;
	
	private boolean mirrored;
	
	private String playerName;
	private Fighter player;
	
	private UnicodeFont uFont = FontContainer.getGameStatusFont();
	private Image healthBar = null;
	private Image healthBarBorder = null;
	private Image energyBar = null;
	private Image energyBarBorder = null;
	
	
	StatusMeter(String playerName, Fighter player, boolean mirrored) throws SlickException, FileNotFoundException {
		this.playerName = playerName;
		this.player = player;
		
		this.mirrored = mirrored;
		
		PositionOffset offset = mirrored ? PositionOffset.TOP_RIGHT : PositionOffset.TOP_LEFT;
		horOff = (float) (0.5 * (offset.ordinal() % 3));
		verOff = (float) (0.5 * ((int)offset.ordinal() / 3)); // OBS! Detta �r korrekt!
		
		healthBar = new Image(ResLoc.getResource("InGame.healthBar"));
		healthBarBorder = new Image(ResLoc.getResource("InGame.healthBarBorder"));
		energyBar = new Image(ResLoc.getResource("InGame.energyBar"));
		energyBarBorder = new Image(ResLoc.getResource("InGame.energyBarBorder"));
		if(mirrored) {
			healthBar = healthBar.getFlippedCopy(true, false);
			healthBarBorder = healthBarBorder.getFlippedCopy(true, false);
			energyBar = energyBar.getFlippedCopy(true, false);
			energyBarBorder = energyBarBorder.getFlippedCopy(true, false);
		}
	}
	
	void draw(int windowWidth, int windowHeight) {
		
		int width = windowWidth;
		int height = windowHeight;
		float scaleX = (float)width / 800f;
		float scaleY = (float)height / 600f;
		
		int posX = mirrored ? 790 : 10;
		int fontPosY = 10;
		
		int hbPosY = (int) (fontPosY + uFont.getLineHeight() + 5);
		int ebPosY = (int) (hbPosY + (healthBar.getHeight() + 5)); 
		
		
		// Print character name.
		uFont.drawString(posX * scaleX - horOff * uFont.getWidth(playerName), 
						 fontPosY * scaleY - verOff * uFont.getLineHeight(), 
						 playerName, 
						 new Color(250, 91, 4));
		
		// Draw healthbar.
		IinGame model = ModelFactory.getViewModel().getInGameState();
		double filled = player.getPlayer() == Fighter.Player.PLAYER1 ? model.getPlayerOneHealth() : model.getPlayerTwoHealth();
		
		Image clipped = healthBar.getSubImage(0, 
											  0, 
											  (int) (healthBar.getWidth() * filled), 
											  healthBar.getHeight());
		
		clipped.draw(posX * scaleX - horOff * clipped.getWidth(),
					 hbPosY * scaleY - verOff * clipped.getHeight(),
					 clipped.getWidth() * scaleX,
					 clipped.getHeight() * scaleY);
		healthBarBorder.draw(posX * scaleX - horOff * healthBarBorder.getWidth(),
							 hbPosY * scaleY - verOff * healthBarBorder.getHeight(),
							 healthBarBorder.getWidth() * scaleX,
							 healthBarBorder.getHeight() * scaleY);
		
		// Draw energybar.
		filled = player.getPlayer() == Fighter.Player.PLAYER1 ? model.getPlayerOneEnergy() : model.getPlayerTwoEnergy();
		clipped = energyBar.getSubImage(0, 
										0, 
										(int) (energyBar.getWidth() * filled), 
										energyBar.getHeight());
		
		clipped.draw(posX * scaleX - horOff * clipped.getWidth(),
					 ebPosY * scaleY - verOff * clipped.getHeight(),
					 clipped.getWidth() * scaleX,
					 clipped.getHeight() * scaleY);
		
		energyBarBorder.draw(posX * scaleX - horOff * energyBarBorder.getWidth(),
							 ebPosY * scaleY - verOff * energyBarBorder.getHeight(),
							 energyBarBorder.getWidth() * scaleX,
							 energyBarBorder.getHeight() * scaleY);
		
	}
}
